package com.newbee.activity;

import java.io.File;

import org.jivesoftware.smack.SmackAndroid;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.hhu.newbee.R;
import com.newbee.activity.tab.MainActivity;
import com.newbee.app.SpeechApp;
import com.newbee.service.MainService;

public class LogoActivity extends Activity
{

	ImageView IV = null;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.activity_logo);
		Typeface typeFace = Typeface.createFromAsset(getAssets(),
				"fonts/zhxk.ttf");
		((TextView) findViewById(R.id.tempsappname)).setTypeface(typeFace);
		TextView slogin = ((TextView) findViewById(R.id.slogin));
		slogin.setTypeface(typeFace);
		slogin.setText("你还在忙着记笔记吗?\n那你就out了！");

		SmackAndroid.init(this);
		SpeechApp.add(this);
		IV = (ImageView) this.findViewById(R.id.animation_logo_id);

		File file = new File(Environment.getExternalStorageDirectory()
				+ SpeechApp.HEAD_FILE);
		if (!file.exists())
		{
			file.mkdirs();
		}

		this.startService(new Intent(this, MainService.class));

		Animation animation = new AlphaAnimation(0.4f, 1.0f);

		animation.setDuration(2000);
		animation.setAnimationListener(new AnimationListener()
		{

			public void onAnimationStart(Animation animation)
			{

			}

			public void onAnimationRepeat(Animation animation)
			{

			}

			public void onAnimationEnd(Animation animation)
			{
				Intent intent = new Intent(LogoActivity.this,
						MainActivity.class);
				LogoActivity.this.startActivity(intent);

			}
		});

		IV.setAnimation(animation);

		initLogStatus();
	}

	private void initLogStatus()
	{
		SharedPreferences sp = this.getSharedPreferences(
				MainActivity.SP_USR_LOGIN_STATUS, Activity.MODE_MULTI_PROCESS);
		Editor editor = sp.edit();
		editor.putBoolean(MainActivity.LOGIN_STATUE, false);
		editor.apply();
	}

	@Override
	protected void onDestroy()
	{
		Intent intent = new Intent();
		intent.setAction(MainService.STOP_SERVICE);
		intent.putExtra(MainService.CMD, 0);
		sendBroadcast(intent);
		super.onDestroy();
	}

	public boolean onKeyDown(int keyCode, KeyEvent event)
	{

		if (keyCode == KeyEvent.KEYCODE_BACK)
		{

			SpeechApp.exit();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
